package module01_work;

import java.util.Arrays;

/**
 * 
 * @author 陈洛健
 * 需求：自定义数组扩容 当已存储元素数量达到总容量的80%时，扩容1.5倍
 * 
 *
 */
public class Work04 {

	private int[] arr;
	//默认值为10
	private int arrayLength;
	private int size;

	public Work04(int length) {
		this.arrayLength = length;
		arr = new int[length];
	}

	public Work04() {
		super();
		this.arrayLength = 10;
		arr = new int[arrayLength];
	}
	
	
	public int getSize() {
		return size;
	}

	/**
	 * 添加元素 添加后size++ size表示数组中元素的数量
	 * 并且判断是否需要扩容 
	 * @param item 插入的元素
	 */
	public void add(int item) {
		arr[size] = item;
		size++;
		
		if(size>= arrayLength * 0.8) {
			int[] newArray = Arrays.copyOf(arr, (int) (arrayLength*1.5));
			System.out.println(Arrays.toString(newArray));
			this.arr = newArray;
		}
	}
	
}
